using UnityEngine;
using System.Collections;

public class roomCreate : MonoBehaviour {
	
	public Texture aTexture;
	public int minWidth = 0;
	public int maxWidth = 100;
	public int minHeight = 0;
	public int maxHeight = 100;
	public int dunWidth = 0;
	public int dunMaxWidth;
	public GUIText textfield;
	public int widthChoice;
	public int dunHeight = 0;
	public int dunMaxHeight = 800;
	public int heightChoice;
	public int numRooms;
	public int numSplits = 0;
	public int splitSpot;
	private Rect leftChild;     
	private Rect rightChild;     
	private Rect dungeon; 
	private static Random rnd = new Random(); 
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	void OnGUI() {
		 if (GUI.Button(new Rect(10, 10, 100, 30), "generate Rooms")) {
			if(numSplits == 0) {
				numRooms = 1;
			}
			else {
				numRooms = numSplits * 2;
			}
			dunMaxWidth = numSplits*2;
			for(int i = 0; i < numRooms || dunWidth <= dunMaxWidth || dunHeight <= dunMaxHeight; i++) {
				print("this loop is being read.");
				widthChoice = Random.Range(minWidth, maxWidth);
				heightChoice = Random.Range(minHeight, maxHeight);
				Graphics.DrawTexture(new Rect(dunWidth, dunHeight,widthChoice,heightChoice),aTexture);
				dunWidth += widthChoice + 5;
				dunHeight += heightChoice + 5;
			}
		}
		int.parse(dunMaxWidth);
		GUI.TextField(new Rect(10, 40, 100, 20), dunMaxWidth, 25);
		textfield.text = "0";
		dunMaxHeight = int.Parse(textfield.text);
		textfield.text = "0";
		minWidth = int.Parse(textfield.text);
		textfield.text = "0";
		minHeight = int.Parse(textfield.text);
		textfield.text = "0";
		maxWidth = int.Parse(textfield.text);
		textfield.text = "0";
		maxHeight = int.Parse(textfield.text);
		textfield.text = "0";
		splitSpot = int.Parse(textfield.text);
		textfield.text = "0";
		numSplits = int.Parse(textfield.text);
		textfield.text = "0";
		if(GUI.Button(new Rect(10,300,100,30), "Split rooms")) {
			split();
		}
		
		if(GUI.Button(new Rect(10,350,100,30), "Reset Dungeon")) {
			numSplits = 0;
		}
	}
	
	public void Rectangle(int minHeight, int minWidth, int dunHeight, int dunWidth) {         
		this.minHeight = minHeight;         
		this.minWidth = minWidth;         
		this.dunWidth = dunWidth;         
		this.dunHeight = dunHeight;   
	} 
	
	public bool split() {                
		bool horizontal; //direction of split    
		if((Random.Range(0,1)) >= 0.5) {
			horizontal = true;
		}
		else {
			horizontal = false;
		}
		int max = (horizontal ? dunHeight : dunWidth ) - splitSpot; //maximum height/width we can split off         
		if( max <= splitSpot ) // area too small to split, bail out             
			return false;         
		int split = Random.Range(minWidth, maxWidth); // generate split point          
		if( split < splitSpot )  // adjust split point so there's at least MIN_SIZE in both partitions             
			split = splitSpot;         
		if( horizontal == true) { //populate child areas             
			leftChild = new Rect(minHeight, minWidth, split, dunWidth);              
			rightChild = new Rect(minHeight+split, minWidth, dunHeight-split, dunWidth);         
		} else {             
			leftChild = new Rect(minHeight, minWidth, dunHeight, split );             
			rightChild = new Rect(minHeight, minWidth+split, dunHeight, dunWidth-split );         
		}         
		return true; //split successful     
	}
}
